package com.ebuy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ebuy.dao.ProductMapper;
import com.ebuy.model.Pager;
import com.ebuy.model.Product;



@Controller
@RequestMapping("/product")
public class ProductControl {
	@Autowired
	private ProductMapper productMapper;
    //进入产品列表,并传入值
	@RequestMapping("/list/{srdId}")
	public String list(@PathVariable Integer srdId,Integer pageIndex,Model model){
	//List<Product> product=productMapper.selectBysrdId(srdId);
	//System.out.println(product.get(0));
	//model.addAttribute("product", product);
	//为需要传进sql语句里的参数建一个map放参数（selectCount和selectBypager语句公用）
	Map<String, Object> params = new HashMap<String,Object>();
	//count(此类商品的总数量)方法里只要一个srdId
	params.put("srdId", srdId);
	int count = productMapper.selectCount(params);
	Pager<Product> pager = new Pager<Product>();
	pager.setCount(count);
	//第一次进来的时候pageIndex为1，Pager类里的if==null
	pager.setPageIndex(pageIndex);
	//给map参数集合里的另外2个参数设值，用来给(selectBypager)方法传参
	//当前页这类商品所显示的数量
	params.put("offset", pager.getPageOffset());//从第几个商品开始
	params.put("pageSize", pager.getPageSize());//这页的总数量
	List<Product> product = productMapper.selectByPager(params);
	pager.setDatas(product);
	model.addAttribute("srdId", srdId);
	model.addAttribute("pager", pager);
	return "page/product-list";
		
	}
	//进入产品视图并传入值
	@RequestMapping(value="/view/{pId}",method=RequestMethod.GET)
	public String view(@PathVariable Integer pId,Model model,HttpServletRequest req,HttpServletResponse resp){
		//System.out.println("进入view");
		//Product product=productMapper.selectByPrimaryKey(pId);
		//model.addAttribute("productv", product);
		
		Product product = productMapper.selectByPrimaryKey(pId);
		model.addAttribute("productv", product);
		
		//保存至最近浏览
		StringBuffer buffer = new StringBuffer();
		Cookie[] cks = req.getCookies();
		Cookie cookie = null;
		//防止cookie移除
		if(cks!=null && cks.length>0){
			for(Cookie ck : cks){
				if("ids".equals(ck.getName())){
					if(!ck.getValue().contains(pId+"")){
						buffer.append(ck.getValue());
						buffer.append("-"+pId);
						ck.setValue(buffer.toString());
					}
					//保存cookie
					cookie = ck;
					break;
				}
			}
		}
		if(cookie==null){
			//1.创建cookie
			cookie = new Cookie("ids",pId+"");
		}
		//2.设置周期
		cookie.setMaxAge(60*60*24);
		//3.设置作用域
		cookie.setPath("/");
		//保存cookie
		resp.addCookie(cookie);
		
		return "page/product-view";
	}
	//进入新闻视图
	@RequestMapping("/newsview")
	public String newsview(){
		return "page/news-view";
	}


}
